package com.tao.auto.batchtao;

import com.tao.auto.batchtao.convert.MgtConvert;
import com.tao.auto.batchtao.home.AbstractHomeLoginBaseFlow;
import com.tao.auto.batchtao.home.AbstractHomeRegisterBaseFlow;
import com.tao.auto.batchtao.home.account.HomeAccountQueue;
import com.tao.auto.batchtao.home.schedue.LoginSignSchedue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
@RestController
public class BatchTaoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(BatchTaoApplication.class, args);
    }


    ExecutorService executorService = Executors.newCachedThreadPool();
    @Autowired
    private AbstractHomeLoginBaseFlow abstractHomeLoginBaseFlow;
    @Autowired
    LoginSignSchedue loginSignSchedue;

    @Autowired
    MgtConvert mgtConvert;


    @Override
    public void run(String... args) throws Exception {
        executorService.execute(() -> {
            while (true) {
                abstractHomeLoginBaseFlow.doSomething(null);
            }
        });
    }


    @GetMapping("/register/start")
    public String registerStart() {
        loginSignSchedue.flag.set(true);
        executorService.execute(() -> {
            try {
                loginSignSchedue.register();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return "ok";
    }

    @GetMapping("/register/stop")
    public String registerStop() {
        loginSignSchedue.flag.set(false);
        return "ok";
    }

    @GetMapping("/login/start")
    public String loginStart() {
        loginSignSchedue.flagLogin.set(true);
        executorService.execute(() -> loginSignSchedue.login());
        return "ok";
    }

    @GetMapping("/login/stop")
    public String loginStop() {
        loginSignSchedue.flagLogin.set(false);
        return "ok";
    }

    @GetMapping("/convert")
    public String convert(String phone, @RequestParam(defaultValue = "0", required = false) long number) {
        mgtConvert.mgtConvert(phone, number);
        return "ok";
    }


}
